package com.dream.service.user.web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.dream.common.model.response.ErrorCode;
import com.dream.common.model.result.AppResultBuilder;
import com.dream.service.user.web.manager.ClientManager;
import com.dream.service.user.web.model.Param;
import com.dream.service.user.web.processor.RequestProcessor;
import com.dream.service.user.web.processor.impl.StoreAddrRequestProcessor;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Created by kangd on 2016/3/22.
 */
public class StoreAddrServlet extends HttpServlet {

    RequestProcessor requestProcessor;

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        requestProcessor= ClientManager.getClient("storeAddrRequestProcessor");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(Param.Default_encode);
        resp.setCharacterEncoding(Param.Default_encode);
        JSONObject result = null;
        if(requestProcessor!=null){
            result = requestProcessor.processor(req);
        }else{
            result= new JSONObject();
            AppResultBuilder.buildProcessErrorResult(result, ErrorCode.PROCESS_ERROR.getError_msg(), ErrorCode.PROCESS_ERROR.getError_msg());
        }
        resp.getOutputStream().write(result.toJSONString().getBytes(Charset.forName(Param.Default_encode)));

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
